package com.lianxian.xindong.home.mvp.presenter;

import android.app.Application;

import com.hxb.library.di.scope.FragmentScope;
import com.hxb.library.http.imageloader.ImageLoader;
import com.hxb.library.integration.AppManager;
import com.hxb.library.mvp.BasePresenter;
import com.lianxian.xindong.commonsdk.BuildConfig;
import com.lianxian.xindong.home.mvp.contract.VideoContract;
import com.lianxian.xindong.home.mvp.model.entity.VideoUserBean;
import com.lianxian.xindong.home.mvp.ui.weight.douyin.ListVideoAdapter;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;


/**
 * @作者：hexingbo
 * @时间：07/02/2023
 * @描述：
 */
@FragmentScope
public class VideoPresenter extends BasePresenter<VideoContract.Model, VideoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    List<VideoUserBean> mDatas;
    @Inject
    ListVideoAdapter videoAdapter;

    private int pageCurrent = 0;
    private int preEndIndex;
    /**
     * 总数据大小
     */
    private int mTotalSize = 0;

    @Inject
    public VideoPresenter(VideoContract.Model model, VideoContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    /**
     * 是否还有更多数据
     *
     * @return
     */
    public boolean hasLoadedAllItems() {
//        return !(mTotalSize > mDatas.size());
        return false;
    }

    public void requestDatas(boolean refresh) {
        mRootView.showLoading();
        Observable.timer(3, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (refresh) {
                            mDatas.clear();
                            mRootView.hideLoading();
                        } else {
                            mRootView.endLoadMore();
                        }
                        for (int i = 0; i < 5; i++) {
                            mDatas.add(new VideoUserBean(i + "",
                                    "@第" + (i + 1) + "个视频",
                                    "#描述信息" + (i + 1),
                                    BuildConfig.DEFAULT_HEAD_URL,
                                    "https://txmov2.a.yximgs.com/bs2/newWatermark/MjczMzU3NDA0ODU_zh_3.mp4"));
                        }
                        videoAdapter.notifyDataSetChanged();

                    }
                });

    }
}
